HDU 3081 Marriage Match II (最大流+二分+并查集)

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1085    Accepted Submission(s): 388


Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids. 
Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend. 
Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
 

 

Input
There are several test cases. First is a integer T, means the number of test cases. 
Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).
Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other. 
Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.
 

 

Output
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
 

 

Sample Input
1 4 5 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
 

 

Sample Output
2
 

 

Author
starvae
 

 

Source
 

 

Recommend
lcy
 
 
 
 
 
 
比较好的题目;
我是二分答案的
然后用最大流进行判断。
 
 
//============================================================================
// Name        : HDU.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=220;
int maze[MAXN][MAXN];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
int flow[MAXN][MAXN];
int sap(int start,int end,int nodenum)
{
    memset(cur,0,sizeof(cur));
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memset(flow,0,sizeof(flow));
    int u=pre[start]=start,maxflow=0,aug=-1;
    gap[0]=nodenum;
    while(dis[start]<nodenum)
    {
        loop:
        for(int v=cur[u];v<nodenum;v++)
            if(maze[u][v]-flow[u][v] && dis[u]==dis[v]+1)
            {
                if(aug==-1||aug>maze[u][v]-flow[u][v])aug=maze[u][v]-flow[u][v];
                pre[v]=u;
                u=cur[u]=v;
                if(v==end)
                {
                    maxflow+=aug;
                    for(u=pre[u];v!=start;v=u,u=pre[u])
                    {
                        flow[u][v]+=aug;
                        flow[v][u]-=aug;
                    }
                    aug=-1;
                }
                goto loop;
            }
        int mindis=nodenum-1;
        for(int v=0;v<nodenum;v++)
            if(maze[u][v]-flow[u][v]&&mindis>dis[v])
            {
                cur[u]=v;
                mindis=dis[v];
            }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return maxflow;
}

int F[110];
int find(int x)
{
    if(F[x]==-1)return x;
    else return F[x]=find(F[x]);
}
void bing(int x,int y)
{
    int t1=find(x);
    int t2=find(y);
    if(t1!=t2)F[t1]=t2;
}
int n,m,f;
int a[10010],b[10010];
bool check(int t)
{
    for(int i=1;i<=n;i++)
        maze[0][i]=t;
    for(int i=n+1;i<=2*n;i++)
        maze[i][2*n+1]=t;
    if(sap(0,2*n+1,2*n+2)==t*n)return true;
    else return false;
}
void solve()
{
    memset(maze,0,sizeof(maze));
    for(int i=0;i<m;i++)
        for(int j=1;j<=n;j++)
        {
            if(find(j)==find(a[i]))
                maze[j][b[i]+n]=1;
        }
    int l=0,r=n;
    int ans=0;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    printf("%d\n",ans);
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&f);
        memset(F,-1,sizeof(F));
        for(int i=0;i<m;i++)
            scanf("%d%d",&a[i],&b[i]);
        int u,v;
        while(f--)
        {
            scanf("%d%d",&u,&v);
            bing(u,v);
        }
        solve();
    }
    return 0;
}

 

posted on 2013-05-03 23:35  kuangbin  阅读(638)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: